package com.niiwoo.civet.trade.service.local.invest;

import com.niiwoo.civet.trade.dao.entity.ProjectInvestOrder;
import com.niiwoo.civet.trade.dao.entity.ProjectInvestorPlan;
import com.niiwoo.civet.trade.dao.mapper.ProjectInvestorPlanMapperExt;
import com.niiwoo.civet.trade.dto.response.BackPlanDTO;
import com.niiwoo.civet.trade.dto.response.DayProjectOrderDetailResponseDTO;
import com.niiwoo.civet.trade.dto.response.MonthInvestOrderDetailResponseDTO;
import com.niiwoo.civet.trade.enums.InvestorPlanReceivedStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by huangmin on 2017/12/10.
 */
@Service
public class InvestOrderDetailService {

    @Autowired
    private ProjectInvestorPlanMapperExt projectInvestorPlanMapperExt;


    /**
     * 月标投资订单详情
     */
    public MonthInvestOrderDetailResponseDTO MonthProjectBackingOrderDetail(ProjectInvestOrder projectInvestOrder) {
        MonthInvestOrderDetailResponseDTO responseDTO = new MonthInvestOrderDetailResponseDTO();

        BigDecimal backedAmount = BigDecimal.ZERO;
        List<ProjectInvestorPlan> planList = projectInvestorPlanMapperExt.listProjectInvestorPlan(projectInvestOrder.getProjectId() ,projectInvestOrder.getUserId());
        for(ProjectInvestorPlan investorPlan : planList){
            BackPlanDTO backPlanDTO = new BackPlanDTO();
	        responseDTO.getBackPlanVOList().add(backPlanDTO);
	        backPlanDTO.setCurrentPeriod(backPlanDTO.getCurrentPeriod());
	        backPlanDTO.setCapital(investorPlan.getCapital());
            backPlanDTO.setInterest(investorPlan.getInterest());
            backPlanDTO.setReceivedDate(investorPlan.getReceivedDate());
	        backPlanDTO.setReceivedStatus(this.getInvestPlanStatus(investorPlan));
	        backedAmount = backedAmount.add(investorPlan.getReceivedInterest()).add(investorPlan.getReceivedCapital());
        }
	    BigDecimal backingAmount = projectInvestOrder.getAmount().add(projectInvestOrder.getInterest()).subtract(backedAmount);
	    responseDTO.setRepaymentType(projectInvestOrder.getRepaymentType());
	    responseDTO.setTotalPeriods(projectInvestOrder.getDeadline());
	    responseDTO.setInvestedAmount(projectInvestOrder.getAmount());//总投资
	    responseDTO.setInterest(projectInvestOrder.getInterest());
	    // TODO 是否有加息 0：没有加息 1：有加息
	    responseDTO.setHaveAddInterest(Byte.valueOf("0"));
        responseDTO.setBackingAmount(backingAmount);
        responseDTO.setBackedAmount(backedAmount);
        return responseDTO;
    }

    /**
     * 天标投资订单详情
     */
    public DayProjectOrderDetailResponseDTO DayProjectBackingOrderDetail(ProjectInvestOrder projectInvestOrder) {
        DayProjectOrderDetailResponseDTO responseDTO = new DayProjectOrderDetailResponseDTO();

	    List<ProjectInvestorPlan> planList = projectInvestorPlanMapperExt.listProjectInvestorPlan(projectInvestOrder.getProjectId(), projectInvestOrder.getUserId());
	    ProjectInvestorPlan projectInvestorPlan = planList.get(0);
	    responseDTO.setHaveAddInterest(Byte.valueOf("0")); // TODO 是否有加息 0：没有加息 1：有加息
	    responseDTO.setAddInterest(BigDecimal.ZERO);
	    responseDTO.setRepaymentType(projectInvestOrder.getRepaymentType());
	    responseDTO.setDeadline(projectInvestOrder.getDeadline());
	    responseDTO.setStatus(this.getInvestPlanStatus(projectInvestorPlan));
	    responseDTO.setInvestedAmount(projectInvestOrder.getAmount());
	    responseDTO.setInterest(projectInvestOrder.getInterest());
	    responseDTO.setBackedAmount(projectInvestorPlan.getReceivedCapital().add(projectInvestorPlan.getReceivedInterest()));
	    responseDTO.setBackDate(projectInvestorPlan.getReceivedDate());
	    return responseDTO;
    }

	/**
	 * 获取回款状态
	 *
	 * @param projectInvestorPlan
	 * @return
	 */
	private Byte getInvestPlanStatus(ProjectInvestorPlan projectInvestorPlan) {
		Byte status = Byte.valueOf("0");// 0回款中，1已回款，2回款逾期
		if (InvestorPlanReceivedStatusEnum.NORMAL_RECEIVE.getCode().equals(projectInvestorPlan.getReceivedStatus())
				|| InvestorPlanReceivedStatusEnum.OVERDUE_RECEIVE.getCode().equals(projectInvestorPlan.getReceivedStatus())) {
			status = Byte.valueOf("1");// 已回款
		} else if (new Date().after(projectInvestorPlan.getReceivedDate())) {// 逾期回款中
			status = Byte.valueOf("2");
		}
		return status;
	}

}
